package com.ebupt.migu.music.common.util.excelSheet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 单个sheet, 支持单行/两行表头
 * @Author zhuar
 * @Date 2019.11.18
 */
public class Sheet {
    /** sheet名 */
    private String name;
    /** 作为head的前几行 */
    private String[][] head;
    /** 该sheet的数据 */
    private List<List<Object>> data;

    public Sheet() {
        this.data = new ArrayList<>();
    }

    public Sheet(String name, List<List<Object>> data) {
        this.name = name;
        this.data = data;
    }

    /**
     * 从list里根据column提取需要的数据
     * @param dataList
     * @param columns
     * @param <T>
     * @return
     */
    public <T> Sheet fillData(List<Map<String,T>> dataList, String[] columns){
        for (Map<String, T> map : dataList) {
            List<Object> rowData = new ArrayList<>();
            for (String column : columns) {
                rowData.add(map.get(column));
            }
            this.data.add(rowData);
        }

        return this;
    }
    /**
     * 从list里根据column提取需要的数据
     * @param dataList
     * @param columns
     * @param <T>
     * @return
     */
    public <T> Sheet fillData2(List<HashMap<String,T>> dataList, String[] columns){
        for (Map<String, T> map : dataList) {
            List<Object> rowData = new ArrayList<>();
            for (String column : columns) {
                rowData.add(map.get(column));
            }
            this.data.add(rowData);
        }

        return this;
    }


    public String getName() {
        return name;
    }

    public Sheet setName(String name) {
        this.name = name;
        return this;
    }

    public String[][] getHead() {
        return head;
    }

    /**
     * 表头
     * @param head {{"列1","时间","时间","时间","列3"},{"列1","年","月","日","列3"}}  -- 合并“时间”列
     * @return
     */
    public Sheet setHead(String[][] head) {
        this.head = head;
        return this;
    }

    /**
     * 表头
     * @param head {"列1","时间","时间","时间","列3"}  -- 合并“时间”列
     * @return
     */
    public Sheet setHead(String[] head) {
        this.head = new String[][]{head};
        return this;
    }

    public List<List<Object>> getData() {
        return data;
    }

    public Sheet setData(List<List<Object>> data) {
        this.data = data;
        return this;
    }
}
